package co.edu.udea.iw.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import co.edu.udea.iw.dao.SolicitudDAO;
import co.edu.udea.iw.dto.solicitud;
import co.edu.udea.iw.exception.waDaoException;

public class SolicitudDAOHibernate extends HibernateDaoSupport implements SolicitudDAO {

	
	
	
	@Override
	public solicitud insertar(solicitud s) throws waDaoException {
		Transaction tx = null;
		Session session = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			session.save(s);
			tx.commit();

		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			// session.close();
		}
		return s;
	}

	@Override
	public solicitud modificar(solicitud s) throws waDaoException {
		Transaction tx = null;
		Session session = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			session.update(s);
			tx.commit();

		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			session.close();
		}
		return s;
	}

	@Override
	public void eliminar(solicitud s) throws waDaoException {
		Transaction tx = null;
		Session session = null;
		try {
			session = getSession();
			tx = session.beginTransaction();

			session.delete(s);
			tx.commit();

		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			session.close();
		}

	}

	@Override
	public List<solicitud> Consultar() throws waDaoException {
		List<solicitud> sds = null;
		Session session = null;
		try {
			session = getSession();

			Criteria criteria = session.createCriteria(solicitud.class);
			sds = criteria.list();
		} catch (HibernateException e) {

			throw new waDaoException(e);
		} 
		return sds;
	}

	@Override
	public solicitud obtener(String id_solicitud) throws waDaoException {
		solicitud s = null;
		Session session = null;
		try {
			session = getSession();
			s = (solicitud) session.load(solicitud.class, id_solicitud);
		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			session.close();
		}
		return s;
	}

}
